﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using MvcCarConsume.Domain;
using MvcCarConsume.Domain.Entites;
using System.ComponentModel;
using System.Text.RegularExpressions;



namespace MvcCarConsume.WebUI
{
    public class CarViewController : BaseController
    {
        private ICarService carService;
        //number of items to be display for per page
        private int PageSize = 8;

        public CarViewController(ICarService carService)
        {
            this.carService = carService;
        }

        //
        // GET: /CarView/
        //TO DO:
        //             2. page is ugly 
      /*  public ActionResult Index(
            [DefaultValue("All")]string category,
            [DefaultValue(1)]int page,
            string brand,
            decimal? lp,
            decimal? hp
            )
        {
            VMCarViewIndex content = new VMCarViewIndex();
            //-------------Build the filter
            List<Func<Car, bool>> filters = new List<Func<Car, bool>>();
            if(!string.IsNullOrEmpty(brand))
                filters.Add(car => Regex.IsMatch(car.Brand, string.Format(@"[^\n\r]*{0}[^\n\r]*", brand)));                       //for patternbrand
            if(lp != null)
                filters.Add(car => car.price >= lp);                                                                                                             //for beingprice
            if(hp != null)
                filters.Add(car => car.price <= hp);                                                                                                               //for endprice
            //-------------

            IList<Car> list = carService.CarsWithCurrentCategoryAndFilter(category, filters);
            int total = list.Count;

            content.LoadCategory(category, null, null);
            content.LoadCars(list.Skip(PageSize * (page - 1)).Take(PageSize));
            content.LoadPageInfo(page, total, PageSize);
            content.LoadQueryInfo(brand, lp, hp);

            return View(content);
        }*/

        //the queryinfo is format as categoryid-typeid-lowprice-highprice-page
        public ActionResult Index([DefaultValue("0-0-0-0-1")]string queryinfo)
        {
            string[] param = queryinfo.Split('-');
            if (param.Length != 5)
            {
                throw new HttpException(404, "bad url");
            }
            VMCarViewIndex content = new VMCarViewIndex();
            //-------------Build the filter

            List<Func<Car, bool>> filters = new List<Func<Car, bool>>();
            if (Convert.ToInt16(param[1]) != 0)
                filters.Add(car => car.BrandId == Convert.ToInt16(param[1]));
            if (Convert.ToInt16(param[2]) != 0)
                filters.Add(car => car.Price >= Convert.ToInt16(param[2]));                                                                                                             //for beingprice
            if (Convert.ToInt16(param[3]) != 0)
                filters.Add(car => car.Price <= Convert.ToInt16(param[3]));                                                                                                               //for endprice
            //-------------

            IList<Car> list = carService.CarsWithCurrentCategoryAndFilter(Convert.ToInt16(param[0]), filters);
            int total = list.Count;

            content.LoadCategory(carService.CategoryWithID(Convert.ToInt16(param[0])));
            content.LoadCars(list, Convert.ToInt16(param[4]), PageSize);
            content.LoadPriceInterval(Convert.ToDecimal(param[2]), Convert.ToDecimal(param[3]));
            content.LoadBrandSelectors(carService.Brands, Convert.ToInt16(param[1]));

            return View(content);
        }

        //
        // GET : /CarView/Detail/id
        public ActionResult Detail(int id)
        {
            CarInView content = null;
            var queryResult = carService.CarWithID(id);
            if (queryResult != null)
            {
                content = new CarInView();
                content.LoadCar(queryResult.CarId,queryResult.CarName,queryResult.Price,queryResult.Description,queryResult.Count);
            }
            return View(content);
        }

        [ChildActionOnly]
        public ViewResult Menu()
        {
            var content = new VMCategoryMenuControl();
            content.Categories.Add(new CategoryInView{
                CategoryID = 0,
                CategoryName = "全部"
            });

            content.LoadCategories(carService.Categories);

            return View("CategoryMenuControl", content);
        }

        //
        //GET: /home
        
        public ActionResult home()
        {
            List<ShowCarInfo> MvcCarDBCars = new List<ShowCarInfo>();
            var cars = carService.GetShowCars();
            foreach(var car in cars)
                MvcCarDBCars.Add(new ShowCarInfo
                {
                    CarId = car.CarId,
                    CarName = car.CarName,
                });
            return View(MvcCarDBCars);
        }

        [ChildActionOnly]
        public ViewResult LanguageMenu()
        {
            var content = new VMLanguageMenu();
            content.InitMenu(Request.Path, CultureProvider.GetCurrentCultureString());
            return View("LanguageMenuControl", content);
        }

    }
}
